package com.uphone.kingmall.activity.personal.set;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.adapter.AddressListAdapter;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.UserAddrBean;
import com.uphone.kingmall.bean.UserAddrsBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.ConstansUtils;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.intent.IntentData;
import com.uphone.kingmall.utils.intent.IntentUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :收货地址
 *     version: 1.0
 * </pre>
 */
public class AddressListActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_address_list)
    RecyclerView rvAddressList;
    @BindView(R.id.btn_add)
    Button btnAdd;
    private AddressListAdapter addressAdapter;
    @IntentData
    private int type = 0;
    private List<UserAddrBean> dataList;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_address_list;
    }

    @Override
    public void initView() {
        setStatusBar();
        rvAddressList.setLayoutManager(new LinearLayoutManager(this));
        addressAdapter = new AddressListAdapter(R.layout.kingmall_item_address);
        rvAddressList.setAdapter(addressAdapter);

        addressAdapter.setOnAddressItemClickListener(new AddressListAdapter.OnAddressItemClickListener() {
            @Override
            public void editItem(View v, int position, UserAddrBean item) {
                IntentUtils.getInstance().with(AddressListActivity.this, EditAddressActivity.class)
                        .putInt(ConstansUtils.ID, item.getId())
                        .putSerializable(ConstansUtils.BEAN, item).start();
            }
        });
        addressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (type == 1) {
                    // 选择地址
                    Intent intent = new Intent()
                            .putExtra("bean", dataList.get(position));
                    setResult(1, intent);
                    finish();
                } else {
                    // 编辑地址
                    IntentUtils.getInstance().with(AddressListActivity.this, EditAddressActivity.class)
                            .putInt(ConstansUtils.ID, dataList.get(position).getId())
                            .putSerializable(ConstansUtils.BEAN, dataList.get(position)).start();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        OkGoUtils.progressRequest(MyUrl.getUserAddrs, this, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    UserAddrsBean bean = GsonUtils.getGson().fromJson(response, UserAddrsBean.class);
                    if (bean != null && bean.getUserAddrs() != null) {
                        dataList = bean.getUserAddrs();
                        addressAdapter.setNewData(dataList);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    @Override
    public void initData() {
    }


    @OnClick({R.id.iv_back, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_add:
                openActivity(EditAddressActivity.class);
                break;
        }
    }
}
